<?php
//error_reporting(0);
	include_once 'isLogin.php';
    
	$table_name = "companies_categories";
	$table_level = "company_levequestion";
	$level="";
	//删除
		//
	if($_GET['action']=='del'){
		echo $commonc->del('company_levequestion',$_POST['id']);
		exit();
	}
	if($_GET['id']!='' && $_GET['id']!=null){
		$level=$_GET['id'];
	}
	$smarty->assign("level",$level);
	//获取大标题问题列表
	$sql=$table_level." where categories_id=".$_GET['id']." and title_level=1";
	$res=$commonc->getList($sql, $sqlnum='', $key="list_id,conent,id",$debug='0');
	//添加
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}
	
    //所有分类层级
	$all_categories_level = $commonc->getList($table_name." order by level ASC",'','level');
	foreach($all_categories_level ["result"] as $values)
	{
		foreach($values as $value)
		{
			
			$levels_unique[] = $value;
		}
	}
	$levels_unique = array_unique($levels_unique);
	$i = 1;
	foreach($levels_unique as $k=>$v)
	{
		unset($levels_unique[$k]);
		$arr['level'] = $v;
		$arr['id'] = $i;
		$levels[] = $arr;
		$i++;
	}
	//$_SERVER['REQUEST_URI'];
	
	
	
	//搜索
	if($_GET['search']!=''&&$_GET['search']!=null)
	{
		$search_key = $_GET['name'];
		$level = $_GET['group_id'];
		
		if($search_key!=''&&$search_key!=null)
		{
			$where =' where'; 
			$where .= " title like '%".$search_key."%' ";
		}
		if($level!=''&&$level!=null)
		{
			if($search_key!=''&&$search_key!=null)
			{
				$where .= " and level = '".$level."'";
			}
			else
			{
				$where =' where'; 
				$where .= " level = '".$level."'";
			}
		}
	}
	else
	{
		//得到所有内容
		if($_GET['id']!=''){
		
			$level = $_GET['level'];
			$level += 1;  
			$upid = $_GET['id'];
			$where = " where upid='".$_GET['id']."'  AND level= '".$level."' ";
		}else{
			$upid = '0';
			$where = " where upid='0' AND level= '".$level."' ";
		}
	}
	
	$smarty->assign("upid",$upid);
	$sql = $table_name;
	$sql.= $where;
	$sql.= " order by sort ASC";
	$row=$commonc->getList($sql,$sql);
	$rowt=$commonc->getList("companies c left join companies_categories g on c.upid=g.sort where c.upid='".$_GET['id']."' order by g.sort DESC", $sqlnum='', $key="c.title as name",$debug='0');
	foreach($rowt as $key=>$value){
	}
	if(is_array($row['result'])){
		//得到是否有下级分类
		
		$row['result'] = $commonc->is_nextcategories($table_name,$colors['state'],$names['state'],$row['result'],'state');
	}
	if ($row['result'][0]['upid']!=''&&$row['result'][0]['upid']!=null) {
	    $smarty->assign("upid",$row['result'][0]['upid']);
	}
	$smarty->assign("res",$res['result']);
	$smarty->assign("row",$row['result']);
	$smarty->assign("value",$value);
	$smarty->assign("levels",$levels);
	
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
?>